<script setup name="Notice">
	import {ref, reactive} from 'vue'
	import {
		Delete,
		Edit,
		Message,
		Search,
		Plus,
		Close
	} from '@element-plus/icons-vue'
	import {ElMessage, ElMessageBox} from "element-plus";
	import {noticeApi} from "@/api/notice.js";
	
	import {useUserInfoStore} from '@/store/userInfo.js'
	
	const userInfoStore = useUserInfoStore();
	let loginUser=userInfoStore.user.role
	//表格数据
	let noticeList = ref([])
	const total = ref(0)
	
	//分页信息，搜索条件
	const noticeQuery = reactive({
		page: 1,
		limit: 10,
		name: '',
		adminName: '',
	})
	
	const loadData = () => {
		noticeApi.list(noticeQuery).then(result => {
			noticeList.value = result.data.list
			total.value = result.data.total
		})
	}
	
	loadData()
	
	const onSearch = () => {
		noticeQuery.page = 1
		loadData()
	}
	
	//单个删除
	const deleteById = (id) => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			.then(() => {
				noticeApi.deleteById(id).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
				
			})
	}
	
	//批量删除
	const ids = ref([])
	const handleSelectionChange = (rows) => {
		ids.value = rows.map(row => row.id)
		console.log(ids.value);
	}
	const deleteAll = () => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			//点击确认删除
			.then(() => {
				ids.value = ids.value.join(',')
				noticeApi.deleteAll(ids.value).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
			})
	}
	
	//添加、编辑
	const dialogVisible = ref(false)
	const notice = ref({})
	const title = ref('')
	const showAddDialog = () => {
		
		dialogVisible.value = true  //弹层出现
		title.value = '添加'
		notice.value = {}
	}
	
	const showUpdateDialog = (id) => {
		dialogVisible.value = true
		title.value = '编辑'
		notice.value = {}
		noticeApi.selectById(id).then(result => {
			notice.value = result.data
		})
	}
	
	const addOrUpdate = () => {
		if (notice.value.id) {  //有值，编辑
			noticeApi.update(notice.value).then(result => {
				if (result.code === 0) {
					ElMessage({message: result.msg, type: 'success',})
					dialogVisible.value = false  //弹层消失
				} else {
					ElMessage.error(result.msg)
				}
				//刷新表格
				loadData()
			})
		} else {   //无值，添加
			noticeApi.add(notice.value).then(result => {
				if (result.code === 0) {
					ElMessage({message: result.msg, type: 'success',})
					dialogVisible.value = false
				} else {
					ElMessage.error(result.msg)
				}
				//刷新表格
				loadData()
			})
		}
	}
	
	//详情
	const dialogDetailsVisible = ref(false)
	const handleClick = (id) => {
		console.log('click')
		noticeApi.selectById(id).then(result => {
			console.log(result.data)
			dialogDetailsVisible.value = true
			notice.value = result.data
		})
	}
	
	//发布公告管理员数据
	import {adminApi} from "@/api/admin.js";
	
	const options = ref([])
	//获取管理员数据
	const selectAllAdmin = () => {
		adminApi.selectAllAdmin().then(result => {
			options.value = result.data
		})
	}
	selectAllAdmin()
	
	//富文本编辑器
	const onEditorChange = (content) => {
		notice.value.content = content
	}
	import WangEditor from "@/components/WangEditor.vue"
	
	//状态更改
	const handleSwitchChange = (row) => {
		noticeApi.updateStatus(row.id, row.status).then(result => {
			if (result.code === 0) {
				ElMessage.success(result.msg)
				loadData()
			} else {
				ElMessage.error(result.msg)
			}
		})
	}
	
	//重置搜索
	const originData = () => ({name: "", adminName: "", beginUpdateTime: "", endUpdateTime: ""});
	const resetForm = () => {
		Object.assign(noticeQuery, originData());
		loadData()
	};
</script>

<template>
	<div class="back">
		<el-card class="">
			<template #header>
				<div class="header">
					<el-button type="success" :icon="Plus" @click="showAddDialog">添加</el-button>
					<el-button type="danger" :icon="Delete" @click="deleteAll">批量删除</el-button>
				</div>
			</template>
			<!--		搜索框-->
			<el-form :inline="true" class="demo-form-inline">
				<el-form-item label="公告标题">
					<el-input v-model="noticeQuery.name" placeholder="请输入公告标题" clearable style='width:160px'/>
				</el-form-item>
				<el-form-item label="发布人员" v-if="loginUser==1">
					<el-select v-model="noticeQuery.adminName" class="m-2"
							   clearable
							   placeholder="请选择发布成员"
							   @change='selectAllAdmin'
							   ar='clearSelect' style='width:160px'>
						<el-option
							v-for="item in options"
							:key="item.id"
							:label="item.name"
							:value="item.name"
						/>
					</el-select>
				</el-form-item>
				<el-form-item label="更新时间">
					<el-date-picker
						v-model="noticeQuery.beginUpdateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="开始时间"
					/>
					-
					<el-date-picker
						v-model="noticeQuery.endUpdateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="结束时间"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" :icon="Search" round @click="onSearch">搜索</el-button>
					<el-button @click="resetForm" round>重置</el-button>
				</el-form-item>
			</el-form>
			
			<!--		信息表格-->
			<el-table ref="multipleTableRef" :data="noticeList" border stripe style="width: 100%;font-size:16px"
					  @selection-change="handleSelectionChange" height="450px">
				<el-table-column type="selection" :selectable="selectable" width="40"/>
				<el-table-column fixed prop="id" label="ID" sortable width="70" align="center" v-if="loginUser==1"/>
				<el-table-column prop="name" label="公告标题" align="center" min-width="140"/>
				<el-table-column prop="content" label="公告内容" align="center" min-width="200" show-overflow-tooltip/>
				<el-table-column prop="adminName" label="发布人员" align="center" width="120" v-if="loginUser==1"/>
				<el-table-column prop="sum" label="确认人数" align="center" width="120"/>
				<el-table-column prop="status" label="状态" align="center" min-width="80">
					<template #default="scope">
						<el-switch
							v-model="scope.row.status"
							class="ml-2"
							inline-prompt
							size="large"
							active-text="通知"
							:active-value="1"
							inactive-text="下架"
							:inactive-value="0"
							@change="handleSwitchChange(scope.row)"
						/>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="创建时间" align="center" width="200"/>
				<el-table-column prop="updateTime" label="更新时间" align="center" width="200"/>
				<el-table-column fixed="right" label="操作" min-width="290" align="center">
					<template #default="{row}">
						<el-button type="info"  :icon="Message" round @click="handleClick(row.id)">详情</el-button>
						<el-button type="primary" v-if="userInfoStore.user.name==row.adminName" :icon="Edit" round @click="showUpdateDialog(row.id)">编辑</el-button>
						<el-button type="danger" v-if="userInfoStore.user.name==row.adminName || userInfoStore.user.role==1" :icon="Delete" round @click="deleteById(row.id)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				v-model:current-page="noticeQuery.page"
				v-model:page-size="noticeQuery.limit"
				:page-sizes="[5, 10, 15, 20]"
				:size="size"
				:disabled="disabled"
				:background="background"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
				@change="loadData"
				style="margin-top: 20px; justify-content: flex-end"
			/>
		</el-card>
		
		<!--		添加编辑弹出框-->
		<el-dialog v-model="dialogVisible" :title=title width="700" :lock-scroll="false">
			<el-form :model="notice">
				<el-form-item label="公告标题：" :label-width="100">
					<el-input v-model="notice.name" autocomplete="off"/>
				</el-form-item>
				<el-form-item label="公告内容：" :label-width="100">
					<WangEditor :initValue="notice.content" @getEditorContent="onEditorChange"
								v-if="dialogVisible" @close="dialogVisible = false">
					</WangEditor>
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="dialogVisible = false">取消</el-button>
					<el-button type="primary" @click="addOrUpdate">
						确认
					</el-button>
				</div>
			</template>
		</el-dialog>
		
<!--		详情框-->
		<el-dialog v-model="dialogDetailsVisible" title="公告详情" style="width: 70%;font-size:16px" :lock-scroll="false">
			<el-descriptions border style="font-size:32px" :data="notice">
				<el-descriptions-item label="公告标题" style="font-size: 20px" align="center" :span="24">
					{{notice.name }}
				</el-descriptions-item>
				<el-descriptions-item label="公告内容" align="center" :span="24">
					<div v-html="notice.content"></div>
				</el-descriptions-item>
				<el-descriptions-item label="操作" align="center" :span="24">
					<el-button type="primary" :icon="Close" round @click="dialogDetailsVisible = false">关闭</el-button>
				</el-descriptions-item>
			</el-descriptions>
		</el-dialog>
	</div>

</template>

<style scoped>

</style>